import {ecs} from '../framework/assets/libs/ecs/ECS';
import {Animation, tween, Vec3, Node} from 'cc';
import { EffectSingleCase } from "../framework/assets/libs/animator-effect/EffectSingleCase";
import {EffectBombComponent, EffectCrushComponent, EffectLayerTurnComponent} from "./utilsComponent";
import { ECSEntity } from '../framework/assets/libs/ecs/ECSEntity';

// 播放动画、动效
export class EffectSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(EffectLayerTurnComponent);
    }

    entityEnter(entity: ECSEntity): void {
        const layer = entity.get(EffectLayerTurnComponent).parent;
        entity.remove(EffectLayerTurnComponent);
        const actions = [];
        let action;
        if(entity.has(EffectCrushComponent)) {
            // 消除
            action = this.effectCrush(layer, entity.get(EffectCrushComponent));
            actions.push(action);
            entity.remove(EffectCrushComponent);
        }
        if (entity.has(EffectBombComponent)) {
            action = this.effectBomb(layer, entity.get(EffectBombComponent));
            actions.push(action);
            entity.remove(EffectBombComponent);
        }
        if (actions.length === 1) {
            // 一个 action 使用 sequence 会报错。。。（智障 cocos）
            actions[0].start();
        } else if (actions.length > 1) {
            tween(layer)
                .sequence(...actions)
                .start();
        }
    }

    // 消除
    effectCrush(layer: Node, model: EffectCrushComponent) {
        const cb = () => {
            const effect = EffectSingleCase.instance.show('common/crush', layer);
            effect.setPosition(new Vec3(model.posX, model.posY, 0));
            const animation = effect.getComponent(Animation);
            animation.play('crush');
            animation.on(Animation.EventType.FINISHED, () => {
                // 删除此动效
                EffectSingleCase.instance.put(effect);
            })
        }
        const action = tween(layer);
        if (model.delayTime) {
            action.delay(model.delayTime);
        }
        return action.call(cb);
    }

    effectBomb(layer: Node, model: EffectBombComponent) {
        console.log('playing bomb, is line bomb', model.isLine);
        let bombName = 'columnBomb';
        if (model.isLine) {
            bombName = 'lineBomb';
        }
        const cb = () => {
            const effect = EffectSingleCase.instance.show('common/' + bombName, layer);
            effect.setPosition(new Vec3(model.posX, model.posY, 0));
            const animation = effect.getComponent(Animation);
            animation.play('bomb');
            animation.on(Animation.EventType.FINISHED, () => {
                // 删除此动效
                EffectSingleCase.instance.put(effect);
            })
        }
        const action = tween(layer);
        if (model.delayTime) {
            action.delay(model.delayTime);
        }
        return action.call(cb);
    }
}
